package il.technion.cs236369.webserver.components;

import java.io.UnsupportedEncodingException;
import java.util.Locale;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.HttpServerConnection;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.EnglishReasonPhraseCatalog;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.protocol.HttpDateGenerator;

public class ServerUtils {
	
	private static HttpDateGenerator httpDateGenerator = new HttpDateGenerator();
	
/*	private static HttpResponse initResponse(int code, String responseEntity){
		
		//HttpResponse res = new BasicHttpResponse(ver, code, EnglishReasonPhraseCatalog.INSTANCE.getReason(code, Locale.ENGLISH));
		HttpResponse res = generateNewServerResponse(code);
		try {
			res.setEntity(new StringEntity(responseEntity));
		} catch (UnsupportedEncodingException e) {
			// Should not be here.
			e.printStackTrace();	// TODO: Remove when done
		}
		return res;
	}*/
	
	/**
	 * Generate HttpResponse with all the basic headers.
	 * @param code - The code of the response. Use HttpStatus to get the correct code.
	 * @return - HttpResponse with all the basic headers.
	 */
	public static HttpResponse generateNewServerResponse(int code){
		HttpResponse res = new BasicHttpResponse(HttpVersion.HTTP_1_1, code, EnglishReasonPhraseCatalog.INSTANCE.getReason(code, Locale.ENGLISH));
		res.setHeader(HttpHeaders.CONNECTION, "close");
		res.setHeader(HttpHeaders.DATE, httpDateGenerator.getCurrentDate());
		return res;
	}

	/**
	 * Sends InternalServerError response and close the connection.
	 * @param connection - connection to the client.
	 */
	public static void sendInternalServerError(HttpServerConnection connection){
		HttpResponse errorResponse503 = getBasicHtmlResponse(HttpStatus.SC_SERVICE_UNAVAILABLE, "The service is temporarly unavilable.");
		sendResponseToClient(connection, errorResponse503);

	}
	
	/**
	 * Sends response to the client and close the connection.
	 * @param connection - The connection to the client.
	 * @param response - The response to be sent.
	 */
	public static void sendResponseToClient(HttpServerConnection connection, HttpResponse response){
		try {
			connection.sendResponseHeader(response);
			connection.sendResponseEntity(response);
		} catch (Exception e) {
			e.printStackTrace(); 	// TODO: Remove when done
		}
		try {connection.close(); } catch(Exception e) {}
	}
	
	public static HttpResponse getBasicHtmlResponse(int code, String message){
		
		String reason = EnglishReasonPhraseCatalog.INSTANCE.getReason(code, Locale.ENGLISH);
		String htmlMessage = "<html> <head> <title>" + 
				code + " " + reason +
				"</title> </head> <body> <h1>" +
				reason + 
				"</h1> <p>" + 
				message + 
				"</p> <hr /> </body> </html>";
		
		HttpResponse res = generateNewServerResponse(code);
		res.setHeader(HttpHeaders.CONTENT_TYPE, "text/html");
		try {
			res.setEntity(new StringEntity(htmlMessage));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace(); // TODO: Remove when done
		}
		
		return res;
	}
}
